package subject_set.offer100;

/**
 * @author haomin
 * @date 2022/10/20 21:22
 **/
public class Offer43 {
    class Solution {
        public int countDigitOne(int n) {
            int ans = 0, digit = 1;
            int high = n/10, cur = n%10, low = 0;
            while(high != 0 || cur != 0){
                if(cur == 0) ans += (high * digit);
                else if(cur == 1) ans += (high * digit + low + 1);
                else ans += ((high + 1) * digit);

                low += cur * digit;
                cur = high % 10;
                high /= 10;
                digit *= 10;
            }
            return ans;
        }
    }
}